
#srcDir = "/home/michael/debs/data/"
#outDir = "/home/michael/debs/out/"
#swcs_outDir = "/home/michael/debs/swc_out/"


#srcDir = "/home/michael/debs/data/"
#outDir = "/home/michael/debs/out/"
#swcs_outDir = "/home/michael/debs/swc_out/"


#srcDir = "N:/BBSRC 2009-12 Measuring Microscope/NEUROMORPH/xyz raw/"
#outDir = "C:/testout/out/"
#swcs_outDir = "C:/testout/swc_out/"

#original way of accessing folders. Now moved to nrnLocations.py
#srcDir = "N:/BBSRC 2009-12 Measuring Microscope/NEUROMORPH/xyz raw/"
#outDir = "N:/BBSRC 2009-12 Measuring Microscope/NEUROMORPH/intermed/"
#swcs_outDir = "N:/BBSRC 2009-12 Measuring Microscope/NEUROMORPH/swc final/"

#Added for Tim's system but unused now
#srcDir = "C:/Users/Tim/Documents/Python Job/Basic 1/Source/"
#outDir = "C:/Users/Tim/Documents/Python Job/Basic 1/Inter/"
#swcs_outDir = "C:/Users/Tim/Documents/Python Job/Basic 1/swc final/"

#look in nrnLocations.py for file paths
from nrnLocations import srcFilesDir as srcDir
from nrnLocations import problemFilenameLoc,problemConversionLoc
from nrnLocations import outFilesDir as outDir
from nrnLocations import swcsFiles_outDir as swcs_outDir

import os


from nrnConcatenate import nrnConcatenateXYZS
from nrnInvertX import nrnInvertX
from nrnStraighten import nrnStraighten
from nrnTranslate import nrnTranslate
from nrnXYZToSWC import nrnXYZToSWC
from nrnScale import nrnScale

import shutil

def nrnDoAll( nrnType, nrnNumber, nrnVersion="",nrnDate=""):
    versionSuffix = "" if not nrnVersion else " "+nrnVersion
    dateSuffix = "" if not nrnDate else " " + nrnDate 
    srcFileNRN = srcDir+"%s %s/%s %s nrn%s%s.txt"%(nrnType,nrnNumber,nrnType,nrnNumber, dateSuffix, versionSuffix)
    srcFileNS =  srcDir+"%s %s/%s %s ns%s%s.txt"%(nrnType,nrnNumber, nrnType,nrnNumber, dateSuffix,  versionSuffix)
    
    print "nrnType = " + nrnType
    print "nrnNumber = " + nrnNumber
    print "versionSuffix = " + versionSuffix
    print "dateSuffix = " + dateSuffix
    
    print "srcFileNRN:" + srcFileNRN
    print "srcFileNS:" + srcFileNS
    
    transFileNRN = outDir+"%s %s/%s %s nrn%s%s.transl.txt"%(nrnType,nrnNumber,nrnType,nrnNumber,dateSuffix,versionSuffix)
    transFileNS = outDir+"%s %s/%s %s ns%s%s.transl.txt"%(nrnType,nrnNumber,nrnType,nrnNumber,dateSuffix,versionSuffix)
    
    print "transFileNRN:" + transFileNRN
    print "transFileNS:" + transFileNS
    
    invTransFileNRN = outDir+"%s %s/%s %s nrn%s%s.transl.invX.txt"%(nrnType,nrnNumber,nrnType,nrnNumber,dateSuffix,versionSuffix)
    invTransFileNS = outDir+"%s %s/%s %s ns%s%s.transl.invX.txt"%(nrnType,nrnNumber,nrnType,nrnNumber,dateSuffix,versionSuffix)

    scaledInvTransFileNRN = outDir+"%s %s/%s %s nrn%s%s.transl.invX.scaled.txt"%(nrnType,nrnNumber,nrnType,nrnNumber,dateSuffix,versionSuffix)
    scaledInvTransFileNS = outDir+"%s %s/%s %s ns%s%s.transl.invX.scaled.txt"%(nrnType,nrnNumber,nrnType,nrnNumber,dateSuffix,versionSuffix)

    
    invTransFileConCat = outDir+"%s %s/%s %s nrn + ns%s%s.transl.invX.scaled.txt"%(nrnType,nrnNumber,nrnType,nrnNumber,dateSuffix,versionSuffix)
    
    invTransFileConCatSWC = outDir+"%s %s/%s %s nrn + ns%s%s.transl.invX.scaled.swc"%(nrnType,nrnNumber,nrnType,nrnNumber,dateSuffix,versionSuffix)
    
    matlabscriptname = outDir+"%s %s/mscript.m"%(nrnType,nrnNumber) #,nrnType,nrnNumber,versionSuffix)
    invTransFileConCatSWCStraight = outDir+"%s %s/%s %s nrn + ns%s%s.transl.invX.scaled.straightened.swc"%(nrnType,nrnNumber,nrnType,nrnNumber,dateSuffix,versionSuffix)
    
    #Old version without unique folder for each cellID
    #final = swcs_outDir+"%s %s nrn + ns%s%s.transl.invX.scaled.straightened.swc"%(nrnType,nrnNumber,dateSuffix,versionSuffix)
    final = swcs_outDir+"%s %s/%s %s nrn + ns%s%s.transl.invX.scaled.straightened.swc"%(nrnType,nrnNumber,nrnType,nrnNumber,dateSuffix,versionSuffix)
    
    print "final: " +  final
    
    print "Source Files"
    print "\t", srcFileNRN
    print "\t", srcFileNS 
    
    print "Producing"
    print "\t",transFileNRN
    print "\t",transFileNS
    
    print "\t",invTransFileNRN
    print "\t",invTransFileNS
    
    print "\t",scaledInvTransFileNRN
    print "\t",scaledInvTransFileNS
    
    print "\t",invTransFileConCat
    print "\t",invTransFileConCatSWC
    print "\t",invTransFileConCatSWCStraight
    
    
    print "Executing:"
    
    
    nrnTranslate(  
        nsFilenameSrc=srcFileNS, 
        nrnFilenameSrc=srcFileNRN, 
        nsFilenameSink=transFileNS, 
        nrnFilenameSink=transFileNRN)
    
    nrnInvertX( srcFilename=transFileNS, sinkFilename=invTransFileNS)
    nrnInvertX( srcFilename=transFileNRN, sinkFilename=invTransFileNRN) 
    
    nrnScale(srcFilename=invTransFileNS, sinkFilename=scaledInvTransFileNS, scaleFactor=1.28)
    nrnScale(srcFilename=invTransFileNRN, sinkFilename=scaledInvTransFileNRN, scaleFactor=1.28)
        
    nrnConcatenateXYZS(  srcFilename1=scaledInvTransFileNRN, srcFilename2=scaledInvTransFileNS, sinkFilename=invTransFileConCat)
    
    nrnXYZToSWC(srcFilename=invTransFileConCat, sinkFilename=invTransFileConCatSWC)
    
    nrnStraighten( srcFilename = invTransFileConCatSWC, sinkFilename=invTransFileConCatSWCStraight,matlabscriptname=matlabscriptname)
    
    #Create Unique output folder for each unique cellID if the folder doesn't exist
    if not os.path.exists(swcs_outDir+"%s %s/"%(nrnType,nrnNumber)): os.makedirs(swcs_outDir+"%s %s/"%(nrnType,nrnNumber))
	
    shutil.copyfile(invTransFileConCatSWCStraight,final)
    
    
